package com.echoesnet.echoesshortvideo.views.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Beans.FansFocusBean;
import com.echoesnet.echoesshortvideo.Persistence.TinyVideoInfoEntity;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.Image.GlideApp;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;


/**
 * Copyright (C) 2017 在线回声（天津）科技发展有限公司
 * 在线回声完全享有此软件的著作权，违者必究
 *
 * @author lc
 * @version 1.0
 * @modifier
 * @createDate 2018/4/25
 * @description
 */
public class FansFollowAdapter extends UltimateViewAdapter<UltimateRecyclerviewViewHolder>
{
    private Activity mAct;
    private List<FansFocusBean> dataList;


    public FansFollowAdapter(Activity act, List<FansFocusBean> list)
    {
        this.mAct = act;
        this.dataList = list;
    }

    @Override
    public UltimateRecyclerviewViewHolder newFooterHolder(View view)
    {
        return new UltimateRecyclerviewViewHolder(view);
    }

    @Override
    public UltimateRecyclerviewViewHolder newHeaderHolder(View view)
    {
        return null;
    }

    @Override
    public UltimateRecyclerviewViewHolder onCreateViewHolder(ViewGroup parent)
    {
        View view = LayoutInflater.from(mAct).inflate(R.layout.fans_and_follow_item, parent, false);
        return new UltimateRecyclerviewViewHolder(view);
    }

    public void setDataList(List<FansFocusBean> dataList)
    {
        this.dataList = dataList;
    }

    @Override
    public int getAdapterItemCount()
    {
        return dataList.size();
    }

    @Override
    public long generateHeaderId(int position)
    {
        return 0;
    }

    @Override
    public void onBindViewHolder(@NonNull UltimateRecyclerviewViewHolder holder, int position)
    {
        if (getItemViewType(position) == VIEW_TYPES.NORMAL) {
            bindDefaultTileItem(position, holder);
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent)
    {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position)
    {

    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    private ItemClickListener onItemClickListener;
    private ItemLongClickListener onItemLongClickListener;

    public void setOnItemClickListener(ItemClickListener listener)
    {
        this.onItemClickListener = listener;
    }

    public interface ItemClickListener
    {
        void onClick(View v, FansFocusBean bean,int position);
    }


    public void setOnItemLongClickListener(ItemLongClickListener listener)
    {
        this.onItemLongClickListener = listener;
    }

    public interface ItemLongClickListener
    {
        void onLongClick(View v, FansFocusBean bean,int position);
    }


    private void bindDefaultTileItem(int position, UltimateRecyclerviewViewHolder holder)
    {
        FansFocusBean fanInfo = dataList.get(position);

        LinearLayout llRoot = holder.getView().findViewById(R.id.ll_root);
        ImageView ivIcon = holder.getView().findViewById(R.id.iv_icon);
        TextView tvNickname = holder.getView().findViewById(R.id.tv_nickname);
        TextView tvSignture = holder.getView().findViewById(R.id.tv_signture);
        TextView tvFocus = holder.getView().findViewById(R.id.tv_focus);


        llRoot.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View v)
            {
                if (onItemLongClickListener!=null)
                    onItemLongClickListener.onLongClick(v,fanInfo,position);
                return false;
            }
        });
        ivIcon.setOnClickListener(v -> {
            if (onItemClickListener != null)
            {
                onItemClickListener.onClick(v, fanInfo,position);
            }
        });

        tvFocus.setOnClickListener(v -> {
            if (onItemClickListener != null)
            {
                onItemClickListener.onClick(v, (FansFocusBean)v.getTag(R.id.viewTagFirst), dataList.indexOf(v.getTag(R.id.viewTagFirst)));
            }
        });
        if (!TextUtils.isEmpty(fanInfo.getNickname()))
        {
            tvNickname.setText(fanInfo.getNickname());
        }
        if (TextUtils.isEmpty(fanInfo.getSignature()))
        {
            tvSignture.setText("Ta还没有想到写点啥~");
        }
        else
        {
            tvSignture.setText(fanInfo.getSignature());
        }
        RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) tvFocus.getLayoutParams();
        if (TextUtils.equals("0", fanInfo.getFollowOrNot()))
        {
            tvFocus.setText("关注");
            params1.width = CommonUtils.dp2px(mAct,60);
            tvFocus.setVisibility(View.VISIBLE);
            tvFocus.setBackgroundResource(R.drawable.round_corner13_red);
        } else if (TextUtils.equals("1", fanInfo.getFollowOrNot()))

        {
            params1.width = CommonUtils.dp2px(mAct,60);
            tvFocus.setText("已关注");
            tvFocus.setVisibility(View.VISIBLE);
            tvFocus.setBackgroundResource(R.drawable.shape_c0323_round13);
        }else if (TextUtils.equals("2", fanInfo.getFollowOrNot()))
        {
            tvFocus.setText("互相关注");
            tvFocus.setBackgroundResource(R.drawable.shape_c0323_round13);
            tvFocus.setVisibility(View.VISIBLE);
            params1.width = CommonUtils.dp2px(mAct,70);

        }else if (TextUtils.isEmpty(fanInfo.getFollowOrNot()))
        {
            tvFocus.setVisibility(View.INVISIBLE);
        }
        tvFocus.setLayoutParams(params1);
        tvFocus.setTag(R.id.viewTagFirst,fanInfo);
        GlideApp.with(AppController.getInstance())
                .load(fanInfo.getHeadPic())
                .circleCrop()
                .error(R.drawable.default_80px)
                .into(ivIcon);


    }
}
